package com.qf.cqut.controller;

import com.qf.cqut.Service.FoodService;
import com.qf.cqut.Service.PlanService;
import com.qf.cqut.model.pojo.AFood;
import com.qf.cqut.model.pojo.APlan;
import com.qf.cqut.result.ResponseCode;
import com.qf.cqut.result.ResponseData;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Controller
@RequestMapping("plans")
@CrossOrigin("*")
public class PlanController {
    @Resource
    private PlanService planService;

//    @Resource
//    private MaterialService materialService;

    @Resource
    private FoodService foodService;

    @RequestMapping("list")
    @ResponseBody
    public ResponseData<List<APlan>> queryAll(Integer page, Integer limit){

        List<APlan> aPlanList = planService.selectByLimit(page,limit);
        Integer count = planService.count();
        if(aPlanList.isEmpty()){
            return new ResponseData<>(ResponseCode.ERROR);
        }
        return new ResponseData<>(ResponseCode.SUCCESS,count,aPlanList);
    }
    @RequestMapping("del")
    @ResponseBody
    public ResponseData delById(String[] ids){
        Integer i = planService.delByIds(ids);
        if(i == 0) {
            return new ResponseData(ResponseCode.ERROR);
        }else{
            return new ResponseData(ResponseCode.SUCCESS);
        }
    }

    @RequestMapping("add")
    @ResponseBody
    public ResponseData addAplan(AFood aFood,APlan aPlan){
        aPlan.setCreateTime(LocalDateTime.now());
        Integer i = planService.addAplan(aPlan);
        Integer j = foodService.executeAplan2(aFood);
        if(i == 0 || j == 0){
            return new ResponseData<>(ResponseCode.ERROR);
        }else{
            return new ResponseData<>(ResponseCode.SUCCESS);
        }
    }

    @RequestMapping("edit")
    @ResponseBody
    public ResponseData editAplan(AFood aFood,APlan aPlan){
        aPlan.setUpdateTime(LocalDateTime.now());
        Integer i = planService.editAplan(aPlan);
        Integer j = foodService.executeAplan(aFood);
        if(i == 0 || j == 0){
            return new ResponseData<>(ResponseCode.ERROR);
        }else{
            return new ResponseData<>(ResponseCode.SUCCESS);
        }
    }
    @RequestMapping("conditions")
    @ResponseBody
    public ResponseData<List<APlan>> queryByConditions(APlan aPlan){
        List<APlan> aPlanList = planService.queryByConditions(aPlan);
        return new ResponseData<>(ResponseCode.SUCCESS,aPlanList);
    }

    @RequestMapping("execute")
    @ResponseBody
    public ResponseData executeAplan(AFood aFood,APlan aPlan){
        aPlan.setCreateTime(LocalDateTime.now());
        Integer i = foodService.executeAplan1(aFood);
        Integer j = planService.addAplan(aPlan);
        if(i == 0||j==0){
            return new ResponseData<>(ResponseCode.ERROR);
        }else{
            return new ResponseData<>(ResponseCode.SUCCESS);
        }
    }
}
